package com.yricky.memoria.ui.dialog

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDialog
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.AlertDialog
import androidx.compose.material.OutlinedTextField
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Clear
import androidx.compose.material.icons.rounded.Clear
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.ViewTreeLifecycleOwner
import androidx.savedstate.ViewTreeSavedStateRegistryOwner
import com.yricky.memoria.ui.theme.ThemeInst

/**
 * @author Yricky
 * @date 2021/12/2
 */

fun showGetStrDialog(context:Context, title:String, defaultValue:String = "", callback:(String)->Unit){
    showComposeDialog(context){
        var inputValue by remember { mutableStateOf(defaultValue)}
        var errorTipText by remember { mutableStateOf("") }
        var showErrorTip by remember { mutableStateOf(false) }

        Column(
            modifier = Modifier
                .wrapContentHeight()
                .padding(24.dp, 12.dp)
        ){
            OutlinedTextField(
                value = inputValue,
                onValueChange = {
                    inputValue = it
                    errorTipText = ""
                    showErrorTip = false
                },
                label = { Text(title) },
                singleLine = true,
                textStyle = TextStyle(color = Color.Gray),
                keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
                keyboardActions = KeyboardActions(
                    onDone = {
                        // 输入回车键等效于确认键
                        if (inputValue == "") {
                            // 判断输入是否为空
                            errorTipText = "输入值不能为空！"
                            showErrorTip = true
                        } else {
                            callback(inputValue)
                            it.dismiss()
                        }
                    }),
                trailingIcon = @Composable {
                    // 输入框右侧的清除图标，点击清空输入
                    Image(imageVector = Icons.Rounded.Clear,
                        contentDescription = null,
                        modifier = Modifier.clickable { inputValue = "" })
                },
                modifier = Modifier.padding(8.dp)
            )
            if(showErrorTip){
                Text(
                    text = errorTipText,
                    color = Color(233, 131, 119, 128),
                    fontSize = 12.sp,
                    textAlign = TextAlign.Left,
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(8.dp, 4.dp, 8.dp, 0.dp)
                )
            }

            Row(
                modifier = Modifier
                    .fillMaxWidth(),
                horizontalArrangement = Arrangement.End
            ) {
                TextButton(
                    onClick = {
                        it.dismiss()
                    }
                ) {
                    Text("取消")
                }
                TextButton(
                    onClick = {
                        if (inputValue == "") {
                            // 判断输入是否为空
                            errorTipText = "输入值不能为空！"
                            showErrorTip = true
                        } else {
                            callback(inputValue)
                            it.dismiss()
                        }
                    }
                ) {
                    Text("确认")
                }


            }
        }
    }
}